// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.editors.editmanager;

import org.dengues.designer.ui.database.figures.DBTableCustomFigure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.widgets.Text;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-9-22 下午11:28:47
 */
public class DiagramChildCellEditorLocator implements CellEditorLocator {

    private IFigure tableFigure;

    int trimX = 15;

    int trimY = 0;

    int trimH = -trimY;

    int trimW = -trimX;

    /**
     * Qiang.Zhang.Adolf@gmail.com DBTableCellEditorLocator constructor comment.
     * 
     * @param tableFigure
     */
    public DiagramChildCellEditorLocator(IFigure tableFigure) {
        this.tableFigure = tableFigure;
    }

    public DiagramChildCellEditorLocator(IFigure tableFigure, int trimX, int trimY, int trimW, int trimH) {
        this.tableFigure = tableFigure;
        this.trimX = trimX;
        this.trimY = trimY;
        this.trimW = trimW;
        this.trimH = trimH;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.tools.CellEditorLocator#relocate(org.eclipse.jface.viewers.CellEditor)
     */
    public void relocate(CellEditor celleditor) {
        Text text = (Text) celleditor.getControl();
        Rectangle rect = tableFigure.getClientArea();
        if (tableFigure instanceof DBTableCustomFigure) {
            rect = ((DBTableCustomFigure) tableFigure).getTitleFigure().getClientArea();
            ((DBTableCustomFigure) tableFigure).getTitleFigure().translateToAbsolute(rect);
        } else {
            tableFigure.translateToAbsolute(rect);
        }
        org.eclipse.swt.graphics.Rectangle trim = text.computeTrim(trimX, trimY, trimW, trimH);
        rect.translate(trim.x, trim.y);
        rect.width += trim.width;
        rect.height += trim.height;
        text.setBounds(rect.x, rect.y, rect.width, rect.height);
    }

}
